/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import sw6.digipecs.database.DBAdapter;
import sw6.digipecs.exceptions.DatabaseException;
import sw6.digipecs.exceptions.IDNotFoundException;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.model.ExtImageInSituation;
import sw6.digipecs.model.Situation;
import sw6.digipecs.widget.admin.HelpView;
import sw6.digipecs.widget.admin.SelectableExtImageAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class SelectPictureActivity extends Activity implements OnItemClickListener {
	private GridView mGrid;
	private Context mContext;
	private Situation mSituation;
	private ArrayList<ExtImageInSituation> mImages;
	private SelectableExtImageAdapter<ExtImageInSituation> mAdapter;
	private Intent mInvokeIntent;
	private HelpView mHelpView;
	public static final String POSITION="position"; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectlockedpicturelayout); 
		mGrid = (GridView) findViewById(R.id.selectlockedpicturegridview);
		mGrid.setOnItemClickListener(this); 
		mContext = getApplicationContext(); 
		mInvokeIntent=getIntent(); 
		mHelpView = (HelpView)findViewById(R.id.helpSelectLockedPicture); 
		mHelpView.setHeadText("Vælg et billede"); 
		mHelpView.setBodyText("Dette bliver løst i sentence stripen"); 
		
		mSituation=null; 
		try {
			try {
				mSituation = new Situation(mContext, Situation.getActiveSituation(mContext));
			} catch (IDNotFoundException e) {
				e.printStackTrace();
			} 
			
		} catch (DatabaseException e) {
			e.printStackTrace();
		} 
		mImages = mSituation.getAllPictures(mContext); 
		mAdapter = new SelectableExtImageAdapter<ExtImageInSituation>(mContext, mImages, 0); 
		mGrid.setAdapter(mAdapter); 
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ExtImageInSituation image = mImages.get(arg2); 
		DBAdapter adapter = new DBAdapter(mContext); 
		adapter.open(); 
		try {
			int iId = image.getId(); 
			int index = (mInvokeIntent.getExtras()).getInt(POSITION); 
			
			adapter.setLockedSentenceBoardItem(index,iId,true); 
		} catch (ImageIsEmptyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		adapter.close(); 
		this.finish(); 
	}
}